package com.ljl.mvvmdemo.ui.activity

import android.content.Intent
import android.widget.Toast
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.ljl.mvvmdemo.GlideApp
import com.ljl.mvvmdemo.R
import com.ljl.mvvmdemo.databinding.ActivitySplashBinding
import com.ljl.mvvmdemo.viewmodel.BaseViewModel
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.Permission


/**
 * 启动页
 * created by luojialun on 2024/11/20
 */
class SplashActivity : BaseActivity<ActivitySplashBinding, BaseViewModel>() {

    override fun getLayoutId(): Int {
        return R.layout.activity_splash
    }

    override fun initView() {
        GlideApp.with(this).asGif().load(R.mipmap.splash).listener(object : RequestListener<GifDrawable> {
            override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<GifDrawable>?, isFirstResource: Boolean): Boolean {
                requestPermission()
                return false
            }

            override fun onResourceReady(resource: GifDrawable?, model: Any?, target: Target<GifDrawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                val gifStateField = GifDrawable::class.java.getDeclaredField("state")
                gifStateField.isAccessible = true
                val gifStateClass = Class.forName("com.bumptech.glide.load.resource.gif.GifDrawable\$GifState")
                val gifFrameLoaderField = gifStateClass.getDeclaredField("frameLoader")
                gifFrameLoaderField.isAccessible = true
                val gifFrameLoaderClass = Class.forName("com.bumptech.glide.load.resource.gif.GifFrameLoader")
                val gifDecoderField = gifFrameLoaderClass.getDeclaredField("gifDecoder")
                gifDecoderField.isAccessible = true
                val gifDecoderClass = Class.forName("com.bumptech.glide.gifdecoder.GifDecoder")
                val gifDecoder: Any = gifDecoderField.get(gifFrameLoaderField.get(gifStateField[resource]))
                val getDelayMethod = gifDecoderClass.getDeclaredMethod("getDelay", Int::class.javaPrimitiveType)
                getDelayMethod.isAccessible = true
                //设置只播放一次
                resource?.setLoopCount(1)
                //获得总帧数
                val count: Int = resource?.frameCount ?: 0
                var delay = 0L
                for (i in 0 until count) {
                    //计算每一帧所需要的时间进行累加
                    delay += getDelayMethod.invoke(gifDecoder, i) as Int
                }
                binding.ivSplash.postDelayed({
                    requestPermission()
                }, delay)

                return false
            }

        }).into(binding.ivSplash)
    }

    fun requestPermission() {
        AndPermission.with(this)
            .permission(Permission.WRITE_EXTERNAL_STORAGE,
                Permission.READ_EXTERNAL_STORAGE,
                Permission.READ_PHONE_STATE)
            .onGranted {
                startActivity(Intent(this, LoginInitActivity::class.java))
                finish()
            }.onDenied {
                Toast.makeText(this, "请授权", Toast.LENGTH_SHORT).show()
            }.start()
    }

}